package com.bw.yk01;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import com.alibaba.ververica.cdc.debezium.StringDebeziumDeserializationSchema;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

//jar->mysql->本类->topic-db
public class FlinkCDC {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DebeziumSourceFunction<String> source = MySQLSource.<String>builder()
                .hostname("hadoop-single")
                .port(3306)
                .databaseList("gmall2021") // set captured database
                .tableList("gmall2021.order_info,gmall2021.order_detail,gmall2021.sku_info,gmall2021.user_info,gmall2021.base_province") // set captured table
                .username("root")
                .password("root")
                .startupOptions(StartupOptions.latest())
                .deserializer(new JsonDebeziumDeserializationSchema()) // converts SourceRecord to JSON String
                .build();

        DataStreamSource<String> stream = env.addSource(source);

        stream.print();

        //TODO sinkTokafka topic-db
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop-single:9092");

        FlinkKafkaProducer<String> myProducer = new FlinkKafkaProducer<>(
                "topic-db",                  // target topic
                new SimpleStringSchema(),    // serialization schema
                properties); // fault-tolerance

        stream.addSink(myProducer);


        env.execute();
    }
}
